package com.web.mall.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.web.mall.pojo.*;
import com.web.mall.pojogroup.TbSpecification;
import com.web.mall.service.TemplateManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class TemplateManagerController {

    @Autowired
    private TemplateManagerService templateManagerService;

    @GetMapping("/queryTemplate")
    public PageResp<TbTypeTemplate> queryTemplate(PageReq pageReq){
        PageHelper.startPage(pageReq.getPage(),pageReq.getSize());
        Page<TbTypeTemplate> page = (Page<TbTypeTemplate>) templateManagerService.queryTemplate();

        PageResp pageResp = new PageResp();
        pageResp.setTotal(page.getTotal());
        pageResp.setList(page.getResult());
        System.out.println(pageReq);
        System.out.println(page);
        return pageResp;
    }

    @GetMapping("/findTemplateList")
    public List<TbTypeTemplate> findTemplateList(){
        return templateManagerService.findTemplateList();
    }


    @GetMapping("/findTemplateById/{id}")
    public Result findTemplateById(@PathVariable Long id){
        TbTypeTemplate tbTypeTemplateById = this.templateManagerService.findTemplateById(id);

        return Result.ok(tbTypeTemplateById);
    }

    @DeleteMapping("/deleteTemplate/{id}")
    public Result deleteTemplate(@PathVariable Long id){
        boolean b= this.templateManagerService.deleteTemplate(id);
        if (b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @PostMapping("/saveTemplate")
    public Result save(@RequestBody TbTypeTemplate tbTypeTemplate){
        System.out.println(tbTypeTemplate.getId());
        boolean b= this.templateManagerService.saveTemplate(tbTypeTemplate);
        if (b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
}
